package com.example.demo.controller;

import com.example.demo.dao.AccountInterface;
import com.example.demo.dao.UserInfoInterface;
import com.example.demo.entity.UserInfo;
import com.example.demo.util.CollectionResponse;
import io.swagger.annotations.Api;

import net.sf.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api("controller相关的报表api")
@RestController
@RequestMapping("/Chart")
public class ChartController {
    @Autowired
    UserInfoInterface userInfoInterface;

    @Autowired
    AccountInterface account;

    @RequestMapping("/MonthNum")
    public String Monthsr(String year, String month, String kind, String mainId) throws JSONException {
        String inrow="0",outrow="0",inMoney,outMoney; //收入数，支出数，收入钱，支出钱
        int years,months,mainIds,kinds;
        //用户详情的预算
        UserInfo userInfo=null;
        JSONObject jsonObject = new JSONObject();
        years = Integer.parseInt(year);
        if(month != null && !month.equals("")){
            months = Integer.parseInt(month);
        }else {
            months=0;
        }
        mainIds = Integer.parseInt(mainId);
        inrow = account.select1(years,months,1,mainIds);
        outrow = account.select1(years,months,0,mainIds);

        inMoney="0";
        if(!inrow.equals("0")){
            inMoney = account.select2(years,months,1,mainIds);
        }
        outMoney="0";
        if(!outrow.equals("0")){
            outMoney = account.select2(years,months,0,mainIds);
        }

        System.out.println("收入："+inrow+"钱："+inMoney);
        System.out.println("支出："+outrow+"钱："+outMoney);
        //用户详情的预算
        userInfo= userInfoInterface.select(mainIds);
        jsonObject.put("budget",userInfo.getBudget());
        jsonObject.put("inrow",inrow);
        jsonObject.put("outrow",outrow);
        jsonObject.put("inMoney",inMoney);
        jsonObject.put("outMoney",outMoney);
        System.out.println("JSON:"+jsonObject);
        String jsonStr=String.valueOf(jsonObject);
        return jsonStr;
    }

    @RequestMapping("/MonthInfo")
    public String MonthInfo(String year, String month, String mainId) throws JSONException {
        String inrow="0",outrow="0",inlastrow="0",oulasttrow="0",inLastMoney,outLastMoney;
        int years,months,mainIds;
        float inMaxDay = 0,inAverage=0,outMaxDay = 0,outAverage=0;
        //用户详情的预算
        UserInfo userInfo=null;
        JSONObject jsonObject = new JSONObject();
        years = Integer.parseInt(year);
        if(month != null && !month.equals("")){
            months = Integer.parseInt(month);
        }else {
            months=0;
        }
        mainIds = Integer.parseInt(mainId);

        //单日最高支出
        inMaxDay= account.SelectMaxDay(years,months,1,mainIds);
        outMaxDay= account.SelectMaxDay(years,months,0,mainIds);
        //日均支出
        inAverage=account.SelectAverageDay(years,months,1,mainIds);
        outAverage=account.SelectAverageDay(years,months,0,mainIds);
        //提前一月的支出和收入
        if(months==1){
            months=12;
            years=years-1;
        }else{
            months=months-1;
        }
        inlastrow = account.select1(years,months,1,mainIds);
        oulasttrow = account.select1(years,months,0,mainIds);
        inLastMoney="0";
        if(!inlastrow.equals("0")){
            inLastMoney = account.select2(years,months,1,mainIds);
        }
        outLastMoney="0";
        if(!oulasttrow.equals("0")){
            outLastMoney = account.select2(years,months,0,mainIds);
        }
        jsonObject.put("inMaxDay",inMaxDay);
        jsonObject.put("inAverage",inAverage);
        jsonObject.put("outMaxDay",outMaxDay);
        jsonObject.put("outAverage",outAverage);
        jsonObject.put("inlastrow",inlastrow);
        jsonObject.put("oulasttrow",oulasttrow);
        jsonObject.put("inLastMoney",inLastMoney);
        jsonObject.put("outLastMoney",outLastMoney);

        System.out.println("JSON:"+jsonObject);
        String jsonStr=String.valueOf(jsonObject);
        return jsonStr;
    }


    @ResponseBody
    @RequestMapping(value = "/Bill", produces = "application/json; charset=utf-8")
    public CollectionResponse mPieChart(String year, String month, String kind, String mainId) throws JSONException {
        CollectionResponse collection = new CollectionResponse();
        CollectionResponse collectionIn = new CollectionResponse();
        CollectionResponse collectionOut = new CollectionResponse();
        int years,months,mainIds;
        String inlistStr="0",outlistStr="0";
        JSONArray jsonin=null,jsonout = null;
        years = Integer.parseInt(year);
        mainIds = Integer.parseInt(mainId);
        if(month != null && !month.equals("")){
            months = Integer.parseInt(month);
        }else {
            months=0;
        }
        System.out.println("年"+year+"月"+months);

        if(kind.equals("1")){
            List<Map<String, Object>> inlist = account.select3(years,months,1,mainIds);
            if(inlist.size()>0 || inlist!=null){
                inlistStr=String.valueOf(inlist);
                System.out.println(inlist);
                jsonin= JSONArray.fromObject(inlist);
                System.out.println("收入："+jsonin);
                collectionIn.setCode("200");
                collectionIn.setData(jsonin);
                collectionIn.setMessage("收入账单");
                System.out.println("收入数据："+collectionIn);
                return collectionIn;
            }
        }else{
            List<Map<String, Object>> outlist = account.select3(years,months,0,mainIds);
            if(outlist.size()>0 || outlist!=null){
                outlistStr=String.valueOf(outlist);
                jsonout= JSONArray.fromObject(outlist);
                System.out.println("支出："+jsonout);
                collectionOut.setCode("200");
                collectionOut.setData(jsonout);
                collectionOut.setMessage("支出账单");
                System.out.println("支出数据："+collectionOut);
                return collectionOut;
            }
        }
        return collection;
    }

    @ResponseBody
    @RequestMapping(value = "/LineChart", produces = "application/json; charset=utf-8")
    public CollectionResponse LineChart(String year, String month, String kind, String mainId) throws JSONException {
        CollectionResponse collection = new CollectionResponse();
        CollectionResponse collectionIn = new CollectionResponse();
        CollectionResponse collectionOut = new CollectionResponse();
        int years,months,mainIds;
        String inlistStr="0",outlistStr="0";
        JSONArray jsonin=null,jsonout = null;
        mainIds = Integer.parseInt(mainId);
        years = Integer.parseInt(year);
        if(month != null && !month.equals("")){
            months = Integer.parseInt(month);
        }else {
            months=0;
        }
        System.out.println(years+"年"+months+"月");

        if(kind.equals("1")){
            List<Map<String, Object>> inlist = account.select4(years,months,1,mainIds);
            if(inlist.size()>0 || inlist!=null){
                inlistStr=String.valueOf(inlist);
                System.out.println(inlist);
                jsonin= JSONArray.fromObject(inlist);
                System.out.println("收入："+jsonin);
                collectionIn.setCode("200");
                collectionIn.setData(jsonin);
                collectionIn.setMessage("收入账单");
                System.out.println("收入数据："+collectionIn);
                return collectionIn;
            }
        }else{
            List<Map<String, Object>> outlist = account.select4(years,months,0,mainIds);
            if(outlist.size()>0 || outlist!=null){
                outlistStr=String.valueOf(outlist);
                jsonout= JSONArray.fromObject(outlist);
                System.out.println("支出："+jsonout);
                collectionOut.setCode("200");
                collectionOut.setData(jsonout);
                collectionOut.setMessage("支出账单");
                System.out.println("支出数据："+collectionOut);
                return collectionOut;
            }
        }
        return collection;
    }

    @RequestMapping("/UpdateMoney")
    public String UpdateMoney(String money, String id) throws JSONException {
        int is=0;
        if(id!=null && money!=null){
            int ids=Integer.parseInt(id);
            float moneys=Float.parseFloat(money);
            is=account.UpdateMoney(ids,moneys);
        }
        return is+"";
    }

//    @ResponseBody
//    @RequestMapping(value = "/BillDetail", produces = "application/json; charset=utf-8")
//    public CollectionResponse BillDetail(String year, String month, String kind, String mainId) throws JSONException {
//        int years,months,mainIds;
//        String inlistStr="0",outlistStr="0";
//        JSONArray jsonin=null,jsonout = null;
//        years = Integer.parseInt(year);
//        months = Integer.parseInt(month);
//        mainIds = Integer.parseInt(mainId);
//        CollectionResponse collection = new CollectionResponse();
//        CollectionResponse collectionIn = new CollectionResponse();
//        CollectionResponse collectionOut = new CollectionResponse();
//        if(kind.equals("1")){
//            List<Map<String, Object>> inlist = account.select3(years,months,1,mainIds);
//            if(inlist.size()>0 || inlist!=null){
//                inlistStr=String.valueOf(inlist);
//                System.out.println(inlist);
//                jsonin= JSONArray.fromObject(inlist);
//                System.out.println("收入："+jsonin);
//                collectionIn.setCode("200");
//                collectionIn.setData(jsonin);
//                collectionIn.setMessage("收入账单");
//                System.out.println("收入数据："+collectionIn);
//                return collectionIn;
//            }
//        }else{
//            List<Map<String, Object>> outlist = account.select3(years,months,0,mainIds);
//            if(outlist.size()>0 || outlist!=null){
//                outlistStr=String.valueOf(outlist);
//                jsonout= JSONArray.fromObject(outlist);
//                System.out.println("支出："+jsonout);
//                collectionOut.setCode("200");
//                collectionOut.setData(jsonout);
//                collectionOut.setMessage("支出账单");
//                System.out.println("支出数据："+collectionOut);
//                return collectionOut;
//            }
//        }
//        return collection;
//    }

}
